angular.module('Controller')

.controller('QgyOffLeaseConfirmCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    if $rootScope.pageType=='off-lease-confirm'
        data=angular.copy($rootScope.pageParams)
        $scope.houseId=data.houseId
        $scope.house=data.house
        $scope.contractId=data.contractId
        $scope.contract=data.contract
        $rootScope.pageParams=undefined
        $rootScope.pageType=undefined
        $scope.checkInfo={
            checkOutDate:undefined
        }
    autoOutDate=()->
        if $scope.contract
            if !$scope.contract.nextLeaseTime
                $scope.checkInfo.checkOutDate=new Date($scope.contract.leaseEnd)
            else
                $scope.checkInfo.checkOutDate=new Date(moment().format('YYYY-MM-DD'))
    loadData=()->
        $scope.offLeaseOver=false
        return if $scope.houseId==undefined
        ##因不同页面进入房间详情时，可能缺少房间明细或相关合同信息，在本处按各情况一一处理
        if $scope.house==undefined
            loadHouse($scope.houseId)
        else if $scope.house.status==2 || $scope.house.status==8
            if $scope.contractId==undefined
                loadContractByHouse($scope.houseId)
            else if $scope.contractId>=0 and $scope.contract==undefined
                loadContractById($scope.contractId)
            else
                autoOutDate()
    loadHouse=(id)->
        return if id==undefined
        $http.get("/house/#{id}/detail")
        .success((resp)->
            if resp.code==0
                $scope.house=resp.message
                if $scope.house.status==2 || $scope.house.status==8
                    if $scope.contractId==undefined
                        loadContractByHouse($scope.houseId)
                    else if $scope.contractId>=0 and $scope.contract==undefined
                        loadContractById($scope.contractId)
        )
    loadContractByHouse=(houseId)->
        $http.get("/contract/house/#{houseId}/statusInfo")
        .success((resp)->
            if resp.code==0
                $scope.contractId=resp.message.contractId
                if $scope.contractId>=0
                    loadContractById($scope.contractId)
        )
    loadContractById=(contractId)->
        $http.get("/contract/#{contractId}/detail")
        .success((resp)->
            if resp.code==0
                $scope.contract=resp.message
                $scope.contract.cotenants=[] if $scope.contract.cotenants==undefined
                autoOutDate()
        )
    loadData()
    $scope.refundType = [
        {
            "value": 1,
            "name": "合同终止"
        },
        {
            "value": 2,
            "name": "承租人原因"
        },
        {
            "value": 3,
            "name": "出租人原因"
        },
        {
            "value": 4,
            "name": "不可抗力"
        },{
            "value": 5,
            "name": "承租人原因但要返还押金"
        }
    ]
    $scope.offLeaseInfo={
        bankName:undefined
        bankAccount:undefined
        bankUser:undefined
        refundType: undefined
        reason:undefined
        money:undefined
    }
    $scope.newConsumption={
        money:undefined
        description:undefined
    }
    $scope.consumptions=[]
    $scope.sexList=commonConstant.qgySexList()
    calReturnMoney=()->
        conSum=0
        _.each($scope.consumptions,(item)->
            conSum+=item.money
        )
        $scope.offLeaseInfo.money=$scope.reSum-conSum
    _.extend $scope,
        loadReturnAmount:()->
            return if $scope.checkInfo.checkOutDate==undefined
            reqData={
                checkOutDate:moment($scope.checkInfo.checkOutDate).format('YYYY-MM-DD')
            }
            $http.get("/contract/checkOut/#{$scope.contractId}/money",params:reqData)
            .success((resp)->
                if resp.code==0
                    $scope.returnInfo=re=resp.message
                    $scope.reSum=re.returnDeposit+re.returnRental+re.returnWater+re.returnPower
                    calReturnMoney()
                else
                    wfAlert.toastError(resp.message)
            )
        resetReturnInfo:()->
            $scope.returnInfo=undefined
            $scope.reSum=undefined
        addConsumption:()->
            sum1=0
            _.each($scope.consumptions,(item)->
                sum1+=item.money
            )
            sum=sum1+$scope.newConsumption.money
            if sum>$scope.returnInfo.returnDeposit
                wfAlert.toastInfo('扣款总额不能大于应退押金')
                return
            else if sum<($scope.returnInfo.returnDeposit - $scope.contract.deposit)
                wfAlert.toastInfo('通过扣款金额减免押金扣除不能超过已扣除押金')
                return
            $scope.consumptions.push(angular.copy($scope.newConsumption))
            $scope.newConsumption={
                money:undefined
                description:undefined
            }
            calReturnMoney()
        removeConsumption:(item)->
            $scope.consumptions=_.without($scope.consumptions,item)
            calReturnMoney()
        printOffLeaseInfo:()->
            return if $scope.offLeaseOver!=true
            _.defer(()->
                $('#off-lease-print-container').print({title:'退租凭单'})
            )
            ''
        confirmOffLease:()->
            reqData=angular.copy($scope.offLeaseInfo)
            reqData.consumptions=$scope.consumptions
            reqData.leaseEnd=moment($scope.checkInfo.checkOutDate).format('YYYY-MM-DD')
            $http.post("/contract/checkOut/#{$scope.contractId}",reqData)
            .success((resp)->
                if resp.code==0
                    $rootScope.$broadcast('refresh')
                    wfAlert.toastInfo('退租确认成功')
                    $scope.offLeaseOver=true
                else
                    wfAlert.toastError(resp.message)
            )
)